package com.java.example.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

/**
 * TODO 基类Fragment
 * @param VB ViewDataBinding
 * @param layoutId 视图Id
 * @author chenll
 * @date 2024/5/17
 */
abstract class BaseFragment<VB : ViewDataBinding>(@LayoutRes private var layoutId: Int) : Fragment() {

    private var bufferRootView: View? = null
    protected lateinit var binding: VB
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        bufferRootView?.let {
            return bufferRootView
        } ?: run {
            binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
            bufferRootView = binding.root
            return bufferRootView
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.unbind()
    }
}
